package com.celesea.workflow.core.vo;

import com.celesea.framework.util.CollectionKit;
import com.celesea.workflow.core.entity.WorkflowNode;
import com.celesea.workflow.core.entity.WorkflowNodeAction;
import com.celesea.workflow.core.entity.WorkflowNodePermission;

import java.util.List;
import java.util.function.Function;

/**
 * @author JackYuan
 * @create 2017-11-28 13:10
 */
public class WorkflowNodeVo extends WorkflowNode {

    private List<WorkflowNodePermission> permissions;
    private List<WorkflowNodeAction> actions;

    public void initActionRelation(Function<List<WorkflowNodeAction>, ?> function) {
        if (CollectionKit.isNotEmpty(actions)) {
            int sort = 0;
            for (WorkflowNodeAction action : actions) {
                action.setWfId(this.getWfId());
                action.setWfNodeId(this.getId());
                action.setSort(sort++);
            }
            function.apply(actions);
        }
    }

    public void initPermissionRelation(Function<List<WorkflowNodePermission>, ?> function) {
        if (CollectionKit.isNotEmpty(permissions)) {
            permissions.stream().forEach(item -> {
                item.setWfId(this.getWfId());
                item.setWfNodeId(this.getId());
            });
            function.apply(permissions);
        }
    }

    public WorkflowNode toEntity() {
        return this;
    }

    public List<WorkflowNodePermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<WorkflowNodePermission> permissions) {
        this.permissions = permissions;
    }

    public List<WorkflowNodeAction> getActions() {
        return actions;
    }

    public void setActions(List<WorkflowNodeAction> actions) {
        this.actions = actions;
    }

}
